package com.lanou.musicproject.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.lanou.musicproject.R;
import com.lanou.musicproject.base.BaseActivity;
import com.lanou.musicproject.utils.MyConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;

import java.util.Map;
import java.util.Set;

/**
 * Created by ztq on 15/10/30.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private Button wxBtn,sinaBtn,qqBtn;
    private Context mContext;
    UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);


        wxBtn = (Button) findViewById(R.id.wxBtn);
        sinaBtn = (Button) findViewById(R.id.sinaBtn);
        qqBtn = (Button) findViewById(R.id.qqBtn);


        wxBtn.setOnClickListener(this);
        sinaBtn.setOnClickListener(this);
        qqBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.wxBtn:

                UMWXHandler wxHandler = new UMWXHandler
                        (this,"wx425e86d793d44993","6e4c225bbb233f443ca9192d062f19f6");
                wxHandler.addToSocialSDK();


                mController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.WEIXIN, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权完成", Toast.LENGTH_SHORT).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.WEIXIN, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(LoginActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                if (status == 200 && info != null) {
                                    StringBuilder sb = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys) {
                                        sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    String name = info.get("screen_name").toString();
                                    String img = info.get("profile_image_url").toString();
                                    MyConfig.putSharePre(LoginActivity.this, "user", "screen_name", name);
                                    MyConfig.putSharePre(LoginActivity.this, "img", "profile_image_url", img);
                                    if (name != null && !name.equals("0") && img != null && !img.equals("0")) {
                                        new MyThread().start();
                                    }
                                    Log.d("TestData", sb.toString());
                                } else {
                                    Log.d("TestData", "发生错误：" + status);
                                }
                            }
                        });
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
                    }
                });

                break;
            case R.id.sinaBtn:


                mController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.SINA,new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                    }
                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        if (value != null && !TextUtils.isEmpty(value.getString("uid"))) {
                            Toast.makeText(LoginActivity.this, "授权成功.",Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(LoginActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onCancel(SHARE_MEDIA platform) {}
                    @Override
                    public void onStart(SHARE_MEDIA platform) {}
                });
                break;
            case R.id.qqBtn:
                UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "100424468",
                        "c7394704798a158208a74ab60104f0ba");
                qqSsoHandler.addToSocialSDK();
                mController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权完成", Toast.LENGTH_SHORT).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(LoginActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                if (status == 200 && info != null) {
                                    StringBuilder sb = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys) {
                                        sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    String name = info.get("screen_name").toString();
                                    String img = info.get("profile_image_url").toString();
                                    MyConfig.putSharePre(LoginActivity.this, "user", "screen_name", name);
                                    MyConfig.putSharePre(LoginActivity.this, "img", "profile_image_url", img);
                                    if (name != null && !name.equals("0") && img != null && !img.equals("0")) {
                                        new MyThread().start();
                                    }
                                    Log.d("TestData", sb.toString());
                                } else {
                                    Log.d("TestData", "发生错误：" + status);
                                }
                            }
                        });
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(LoginActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
                    }
                });

                break;

        }
    }
    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                sleep(2000);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
